package org.gwtmultipage.sample.client;

import hr.fer.zemris.rznu.model.dto.betting.BettingEventDTO;
import hr.fer.zemris.rznu.model.dto.user.UserDTO;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.gwtmultipage.sample.client.BettingService;
import org.gwtmultipage.sample.client.BettingServiceAsync;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import org.gwtmultipage.client.UrlPatternEntryPoint;
import com.google.gwt.user.client.ui.MenuItemSeparator;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;

/**
 * @author brian.ireilly
 */

@UrlPatternEntryPoint("Bets.html(\\\\?.*)?")
public class Bets implements EntryPoint {
	
	private RootPanel wrapper;
	private VerticalPanel preContainer;
	private FlowPanel footer;
	private FlowPanel banner;
	private FlowPanel content;
	private FlowPanel header;
	private LayoutPanel container;
	private VerticalPanel loginContainer;
	private FlowPanel TopMenu;
	private MenuBar menuBar;
	private FlowPanel contentTop;
	private FlowPanel content1;
	private Label labelFooter;
	private MenuItem naslovnica;
	private MenuItemSeparator separator;
	private MenuItem ponuda;
	private MenuItemSeparator separator_1;
	private MenuItem rezultati;
	private MenuItemSeparator separator_2;
	private MenuItem statistika;
	private MenuBar menuBar_1;
	private HorizontalPanel login;
	private FlexTable flexTable;
	private FlexTable flexTable1;
	private FlexTable flexTable2;
	private FlexTable flexTable3;
	private Label username;
	private TextBox textBoxUsername;
	private Label password;
	private PasswordTextBox textBoxPassword;
	private Button btnLogIn;
	private Button btnRegister;
	private Image imageHr;
	private Image imageEng;
	private LayoutPanel leftSection;
	private LayoutPanel rightSection;
	private ScrollPanel ticket;
	private ScrollPanel scrollPanel;
	private Label user;
	private Label bankValue;
	private Button btnLogOut;
	private Button btnProfile;
	
	Double possibleWin=0.;
	Integer xPositionBet=0;
	Double totalOdd=1.;
	Double stake=5.;
	
	Map pair = new HashMap(20);
	
	private BettingConstants constants = GWT.create(BettingConstants.class);
	private BettingMessages messages = GWT.create(BettingMessages.class);

	private final BettingServiceAsync bettingService = GWT.create(BettingService.class);
	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	private final InternationalizationServiceAsync interService = GWT.create(InternationalizationService.class);
	
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";
		
	
		public void addPair(){
		HorizontalPanel parPanel =new HorizontalPanel();
		Label pairs = new Label(constants.number() + ":");
		pairs.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		parPanel.add(pairs);
		Label numBet = new Label(xPositionBet.toString());
		numBet.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		parPanel.add(numBet);
		flexTable2.setWidget(2, 0, parPanel);
		parPanel.setSize("230", "20");
	}
	
	public void deletePair(){
		HorizontalPanel parPanel =new HorizontalPanel();
		Label pairs = new Label(constants.number() + ":");
		pairs.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		parPanel.add(pairs);
		xPositionBet--;
		Label numBet = new Label(xPositionBet.toString());
		numBet.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		parPanel.add(numBet);
		flexTable2.setWidget(2, 0, parPanel);
		parPanel.setSize("230", "20");		
	}
	
	public void addOdd(){
		HorizontalPanel oddPanel =new HorizontalPanel();
		Label oddL = new Label(" " + constants.totalOdds()+ " : ");
		oddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		oddPanel.add(oddL);
		Label totalOddL = new Label(totalOdd.toString());
		totalOddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		oddPanel.add(totalOddL);
		flexTable2.setWidget(4, 0, oddPanel);
		oddPanel.setSize("230", "20");
	}
	
	public void deleteOdd(double odd){
		HorizontalPanel oddPanel =new HorizontalPanel();
		Label oddL = new Label(" " + constants.totalOdds()+ " : ");
		oddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		oddPanel.add(oddL);
		totalOdd=totalOdd / odd;
		if (xPositionBet==0){
			totalOdd=1.0;
		}
		totalOdd=(double)Math.round(totalOdd * 10000) / 10000;
		Label totalOddL = new Label(totalOdd.toString());
		totalOddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		oddPanel.add(totalOddL);
		flexTable2.setWidget(4, 0, oddPanel);
		oddPanel.setSize("230", "20");
		addWinning();
			
	}
	
	public void addWinning(){
	    HorizontalPanel winningPanel =new HorizontalPanel();
		Label win = new Label(" " + constants.possibleWinnings()+ " : ");
		win.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		winningPanel.add(win);
		double possibleWin1= totalOdd * stake;
		if (possibleWin1 > 2000000.0) {
			maxWinDialogBox();
		}
		else {
			possibleWin=possibleWin1;
			possibleWin=(double)Math.round(possibleWin * 100) / 100;
			Label possibleW = new Label(possibleWin.toString());
			possibleW.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			winningPanel.add(possibleW);
			flexTable2.setWidget(5, 0, winningPanel);
			winningPanel.setSize("230", "20");
		}
	}
	
	public void maxBetsDialogBox(){
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText(messages.maxNumb());
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button(constants.close());
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML(messages.maxNumbMessage()));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		dialogBox.center();
		closeButton.setFocus(true);
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
	}
	
	public void maxWinDialogBox(){
		final DialogBox maxWinDialogBox = new DialogBox();
		maxWinDialogBox.setText(messages.maxWin());
		maxWinDialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button(constants.close());
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML(messages.maxWinMessage()));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		maxWinDialogBox.setWidget(dialogVPanel);
		maxWinDialogBox.center();
		closeButton.setFocus(true);
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				maxWinDialogBox.hide();
			}
		});
	}
	
	public void samePairDialogBox(){
		final DialogBox samePairDialogBox = new DialogBox();
		samePairDialogBox.setText(messages.samePair());
		samePairDialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button(constants.close());
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>" + messages.samePairMessage()));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		samePairDialogBox.setWidget(dialogVPanel);
		samePairDialogBox.center();
		closeButton.setFocus(true);
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				samePairDialogBox.hide();
			}
		});
	}
	
	public void addCloseButton (){
		final Button closeButton1 = new Button("X");
		closeButton1.setStyleName("gwt-SignIn");
		flexTable3.setWidget(xPositionBet, 3, closeButton1);
		flexTable1.getCellFormatter().setHorizontalAlignment(xPositionBet, 3, HasHorizontalAlignment.ALIGN_CENTER);
		closeButton1.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Cell cell = flexTable3.getCellForEvent(event);
				int row = cell.getRowIndex();
				String id = flexTable3.getText(row,0);
				pair.remove(id);
				if(pair.containsKey(id)){
					samePairDialogBox();
				}
				flexTable3.removeRow(row);
				deletePair();
				String delete = flexTable3.getText(row,2);
		    	double deleteOdd1 =Double.parseDouble(delete);
		    	deleteOdd(deleteOdd1);
		    			
			}
		});
	}


	public void onModuleLoad() {

		wrapper = RootPanel.get();
		wrapper.setStyleName("gwt-Template-wrapper");
		wrapper.setSize("100%", "900px");
		
		preContainer = new VerticalPanel();
		preContainer.setBorderWidth(0);
		preContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		wrapper.add(preContainer, 0, 0);
		preContainer.setSize("100%", "900px");
		
		container = new LayoutPanel();
		preContainer.add(container);
		preContainer.setCellHorizontalAlignment(container, HasHorizontalAlignment.ALIGN_CENTER);
		container.setStyleName("gwt-Template-container");
		container.setSize("960", "900 px");
		
		header = new FlowPanel();
		header.setStyleName("gwt-Template-header");
		container.add(header);
		header.setWidth("958");
		container.setWidgetLeftWidth(header, 0.0, Unit.PX, 964.0, Unit.PX);
		container.setWidgetTopHeight(header, 0.0, Unit.PX, 125.0, Unit.PX);
		
		loginContainer = new VerticalPanel();
		loginContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		loginContainer.setStyleName("gwt-Template-topmenu");
		header.add(loginContainer);
		
		login = new HorizontalPanel();
		login.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		login.setSize("700", "35");
		loginContainer.add(login);
		
		flexTable = new FlexTable();
		login.add(flexTable);
		login.setCellHorizontalAlignment(flexTable, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setSize("466px", "35px");
		
		isLogged();
									
		TopMenu = new FlowPanel();
		TopMenu.setStyleName("gwt-Template-login");
		header.add(TopMenu);
		
		imageHr = new Image("images/hrvatski.gif");
		TopMenu.add(imageHr);
		imageHr.setStyleName("gwt-ImageHr");
		imageHr.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				interService.currentLanguage("hr", new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(Void result) {
						Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						
					}});
			}
		});
		
		imageEng = new Image("images/engleski.gif");
		TopMenu.add(imageEng);
		imageEng.setStyleName("gwt-ImageHr");
		imageEng.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				interService.currentLanguage("eng", new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(Void result) {
						Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						
					}});
			}
		});
			
		menuBar = new MenuBar(false);
		menuBar.setAnimationEnabled(true);
		TopMenu.add(menuBar);
		menuBar.setSize("360", "22");
		
		naslovnica = new MenuItem(constants.home(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						if (result.equals("eng") || result.equals("null")) {
							Window.open("http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
						
					}});
			}
		});
		menuBar.addItem(naslovnica);
		naslovnica.setWidth("90");
		
		separator = new MenuItemSeparator();
		menuBar.addSeparator(separator);
		
		ponuda = new MenuItem(constants.bets(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						if(result.equals("eng") || result.equals("null")) {
							Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
					}});
			}
		});
		menuBar.addItem(ponuda);
		ponuda.setWidth("90");
		
		separator_1 = new MenuItemSeparator();
		menuBar.addSeparator(separator_1);
		
		rezultati = new MenuItem(constants.results(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						if(result.equals("eng")|| result.equals("null")) {
							Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/Results.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
						
					}});
			}
		});
		menuBar.addItem(rezultati);
		rezultati.setWidth("90");
		
		separator_2 = new MenuItemSeparator();
		menuBar.addSeparator(separator_2);
		
		statistika = new MenuItem(constants.statistic(), false, new Command() {
			public void execute() {
				interService.checkCurrentLanguage(new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error " + caught.getMessage());
						
					}

					@Override
					public void onSuccess(String result) {
						if(result.equals("eng") || result.equals("null")) {
							Window.open("http://127.0.0.1:8888/Statistic.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
						}
						else if(result.equals("hr")) {
							Window.open("http://127.0.0.1:8888/Statistic.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
						}
						
					}});
			}
		});
		menuBar.addItem(statistika);
		statistika.setWidth("90");
		
		banner = new FlowPanel();
		banner.setStyleName("gwt-Template-banner");
		container.add(banner);
		banner.setSize("938", "195");
		container.setWidgetLeftWidth(banner, 11.0, Unit.PX, 960.0, Unit.PX);
		container.setWidgetTopHeight(banner, 106.0, Unit.PX, 250.0, Unit.PX);
		
		content = new FlowPanel();
		content.setSize("956", "100%");
		content.setStyleName("gwt-Template-Content");
		container.add(content);
		container.setWidgetLeftWidth(content, 0.0, Unit.PX, 960.0, Unit.PX);
		container.setWidgetTopHeight(content, 309.0, Unit.PX, 500.0, Unit.PX);
		
		content1 = new FlowPanel();
		content1.setSize("", "100%");
		content1.setStyleName("gwt-Template-Content1");
		content.add(content1);
		
		leftSection = new LayoutPanel();
		leftSection.setStyleName("gwt-Template-section-left");
		content1.add(leftSection);
		leftSection.setSize("556", "420");
	

		menuBar_1 = new MenuBar(false);
		menuBar_1.setAnimationEnabled(true);
		leftSection.add(menuBar_1);
		menuBar_1.setSize("556", "20");

		scrollPanel = new ScrollPanel();
		scrollPanel.setSize("556", "400");
		    
		flexTable1= new FlexTable();
		flexTable1.setStyleName("customers");
		scrollPanel.add(flexTable1);
		flexTable1.setBorderWidth(2);
		flexTable1.setWidth("100%");
		Label label = new Label("");
		label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		flexTable1.setWidget(0, 0, label);
	    Label label_1 = new Label("");
	    label_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    flexTable1.setWidget(0, 1, label_1);
	    Label label_2 = new Label(constants.time());
	    label_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    flexTable1.setWidget(0, 2, label_2);
	    Label label_3 = new Label("1");
	    label_3.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    flexTable1.setWidget(0, 3, label_3);
	    Label label_7 = new Label("X");
	    label_7.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    flexTable1.setWidget(0, 4, label_7);
	    Label label_4 = new Label("2");
	    label_4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    flexTable1.setWidget(0, 5, label_4);
	    Label label_5 = new Label("1X");
	    label_5.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    flexTable1.setWidget(0, 6, label_5);
	    Label label_6 = new Label("X2");
	    label_6.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    flexTable1.setWidget(0, 7, label_6);
	 		
	    leftSection.add(scrollPanel);
		leftSection.setWidgetLeftWidth(scrollPanel, 0.0, Unit.PX, 556.0, Unit.PX);
		leftSection.setWidgetTopHeight(scrollPanel, 23.0, Unit.PX, 420.0, Unit.PX);
	
		bettingService.getEvents(
				new AsyncCallback<List<BettingEventDTO>>() {
					public void onFailure(Throwable caught) {
						Window.alert(SERVER_ERROR);
					}

					public void onSuccess(List<BettingEventDTO> events) {
						Iterator<BettingEventDTO> itr = events.iterator(); 
						int i=1;
					    if (events.size() > 0) {
					    	while(itr.hasNext()) { 
					    		BettingEventDTO element = itr.next(); 
					    		flexTable1.getCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
					    		flexTable1.getCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
					    		flexTable1.getCellFormatter().setHorizontalAlignment(i, 4, HasHorizontalAlignment.ALIGN_CENTER);
					    		flexTable1.getCellFormatter().setHorizontalAlignment(i, 5, HasHorizontalAlignment.ALIGN_CENTER);
					    		flexTable1.getCellFormatter().setHorizontalAlignment(i, 6, HasHorizontalAlignment.ALIGN_CENTER);
					    		flexTable1.getCellFormatter().setHorizontalAlignment(i, 7, HasHorizontalAlignment.ALIGN_CENTER);
						    
					    		for (int j = 0; j < 8; j++) {
					    		
					    			switch (j) {
					    				case 0:  	Label id=new Label(element.getEventId().toString());
					            					flexTable1.setWidget(i, j, id); break;
					            
					    				case 1:  	Label game=new Label(element.getHomeTeam()+"-"+element.getAwayTeam());
					    							flexTable1.setWidget(i, j, game); break;
					            
					    				case 2:  	Label time=new Label(element.getStartTime());
													flexTable1.setWidget(i, j, time); break;
											
					    				case 3:  	Label oddHome=new Label(element.getOddHome().toString());
													flexTable1.setWidget(i, j, oddHome);
													oddHome.addClickHandler(new ClickHandler() {
														public void onClick(ClickEvent event) {
															Cell cell = flexTable1.getCellForEvent(event);
															int row = cell.getRowIndex();
															int col = cell.getCellIndex();
															if (xPositionBet==0){
																flexTable2.setVisible(true);
																}
															if (xPositionBet==20){
																maxBetsDialogBox();
																}
															else {
																String id = flexTable1.getText(row,0);
																if (pair.containsKey(id)){
																	samePairDialogBox();										
																	
																}
																else {
																	Label idLabel=new Label(id);
																	pair.put(id, new Integer(1));
																	idLabel.setSize("50", "20");
																    idLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 0, idLabel);
																	Label oddL= new Label("1");
																    oddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	oddL.setSize("35", "20");
																	flexTable3.setWidget(xPositionBet, 1, oddL);
																	String odd = flexTable1.getText(row,col);
																	Label oddLabel=new Label(odd);
																	oddLabel.setSize("35", "20");
																    oddLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 2, oddLabel);
																	addCloseButton ();
																	xPositionBet++;
														    
																	addPair();
																	double oddDouble = Double.parseDouble(odd);
																	totalOdd=totalOdd*oddDouble;
																	totalOdd=(double)Math.round(totalOdd * 10000) / 10000;

																	addOdd();
																	addWinning();}
																}
															
															}
													});break;
											
					    				case 4:  	Label oddDraw=new Label(element.getOddDraw().toString());
													flexTable1.setWidget(i, j, oddDraw);
													oddDraw.addClickHandler(new ClickHandler() {
														public void onClick(ClickEvent event) {
															Cell cell = flexTable1.getCellForEvent(event);
															int row = cell.getRowIndex();
															int col = cell.getCellIndex();
															if (xPositionBet==0){
																flexTable2.setVisible(true);
																}
															if (xPositionBet==20){
																maxBetsDialogBox();
																}
															else {
																String id = flexTable1.getText(row,0);
																if (pair.containsKey(id)){
																	samePairDialogBox();										
																	
																}
																else {
																	Label idLabel=new Label(id);
																	pair.put(id, new Integer(1));
																	idLabel.setSize("50", "20");
																    idLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 0, idLabel);
																	Label oddL= new Label("X");
																	oddL.setSize("35", "20");
																    oddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 1, oddL);
																	String odd = flexTable1.getText(row,col);
																	Label oddLabel=new Label(odd);
																	oddLabel.setSize("35", "20");
																    oddLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 2, oddLabel);
																	addCloseButton ();
																	xPositionBet++;
															
																	addPair();
																	double oddDouble = Double.parseDouble(odd);
																	totalOdd=totalOdd*oddDouble;
																	totalOdd=(double)Math.round(totalOdd * 10000) / 10000;
																	addOdd();
																	addWinning();}
															}
																													}
													});break;
																					
					    				case 5:  	Label oddAway=new Label(element.getOddAway().toString());
					    							flexTable1.setWidget(i, j, oddAway);
													oddAway.addClickHandler(new ClickHandler() {
														public void onClick(ClickEvent event) {
															Cell cell = flexTable1.getCellForEvent(event);
															int row = cell.getRowIndex();
															int col = cell.getCellIndex();
															if (xPositionBet==0){
																flexTable2.setVisible(true);
																}
															if (xPositionBet==20){
																maxBetsDialogBox();
																}
															else {
																String id = flexTable1.getText(row,0);
																if (pair.containsKey(id)){
																	samePairDialogBox();										
																	
																}
																else {
																	Label idLabel=new Label(id);
																	idLabel.setSize("50", "20");
																    idLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	pair.put(id, new Integer(1));
																	flexTable3.setWidget(xPositionBet, 0, idLabel);
																	Label oddL= new Label("2");
																	oddL.setSize("35", "20");
																    oddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 1, oddL);
																	String odd = flexTable1.getText(row,col);
																	Label oddLabel=new Label(odd);
																	oddLabel.setSize("35", "20");
																    oddLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 2, oddLabel);
																	addCloseButton ();
																	xPositionBet++;
																
																	addPair();
																	double oddDouble = Double.parseDouble(odd);
																	totalOdd=totalOdd*oddDouble;
																	totalOdd=(double)Math.round(totalOdd * 10000) / 10000;
																	addOdd();
																	addWinning();}
															}
															
																													}
													});break;
											
					    				case 6:  	Label oddHomeDraw=new Label(element.getOddHomeDraw().toString());
					    							flexTable1.setWidget(i, j, oddHomeDraw);
													oddHomeDraw.addClickHandler(new ClickHandler() {
														public void onClick(ClickEvent event) {
															Cell cell = flexTable1.getCellForEvent(event);
															int row = cell.getRowIndex();
															int col = cell.getCellIndex();
															if (xPositionBet==0){
																flexTable2.setVisible(true);
																}
															if (xPositionBet==20){
																maxBetsDialogBox();
																}
															else {
																String id = flexTable1.getText(row,0);
																if (pair.containsKey(id)){
																	samePairDialogBox();										
																	
																}
																else {
																	Label idLabel=new Label(id);
																	idLabel.setSize("50", "20");
																    idLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	pair.put(id, new Integer(1));
																	flexTable3.setWidget(xPositionBet, 0, idLabel);
																	Label oddL= new Label("1X");
																	oddL.setSize("35", "20");
																    oddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 1, oddL);
																	String odd = flexTable1.getText(row,col);
																	Label oddLabel=new Label(odd);
																	oddLabel.setSize("35", "20");
																    oddLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 2, oddLabel);
																	addCloseButton ();
																	xPositionBet++;
																	
																	addPair();
																	double oddDouble = Double.parseDouble(odd);
																	totalOdd=totalOdd*oddDouble;
																	totalOdd=(double)Math.round(totalOdd * 10000) / 10000;
																	addOdd();
																	addWinning();}
															}
																													}
													});break;
					            
					    				case 7:  	Label oddDrawAway=new Label(element.getOddDrawAway().toString());
					    							flexTable1.setWidget(i, j, oddDrawAway);
													oddDrawAway.addClickHandler(new ClickHandler() {
														public void onClick(ClickEvent event) {
															Cell cell = flexTable1.getCellForEvent(event);
															int row = cell.getRowIndex();
															int col = cell.getCellIndex();
															if (xPositionBet==0){
																flexTable2.setVisible(true);
																}
															if (xPositionBet==20){
																maxBetsDialogBox();
																}
															else {
																String id = flexTable1.getText(row,0);
																if (pair.containsKey(id)){
																	samePairDialogBox();										
																	
																}
																else {
																	Label idLabel=new Label(id);
																	idLabel.setSize("50", "20");
																    idLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	pair.put(id, new Integer(1));
																	flexTable3.setWidget(xPositionBet, 0, idLabel);
																	Label oddL= new Label("X2");
																	oddL.setSize("35", "20");
																    oddL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 1, oddL);
																	String odd = flexTable1.getText(row,col);
																	Label oddLabel=new Label(odd);
																	oddLabel.setSize("35", "20");
																    oddLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
																	flexTable3.setWidget(xPositionBet, 2, oddLabel);
																	addCloseButton ();
																	xPositionBet++;
															
																	addPair();
																	double oddDouble = Double.parseDouble(odd);
																	totalOdd=totalOdd*oddDouble;
																	totalOdd=(double)Math.round(totalOdd * 10000) / 10000;
																	addOdd();
																	addWinning();}
															}
																													}
													});break;
					            
					    				}

					    			            		    		
					    		 
					    	    
					    		}
					    		i++;
					    		}
					    
					    	}
					}
				});
		
		
		rightSection = new LayoutPanel();
		content1.add(rightSection);
		rightSection.setStyleName("gwt-Template-section-right");
		rightSection.setSize("238", "420");
		
		flexTable2= new FlexTable();
		rightSection.add(flexTable2);
        flexTable2.setVisible(false);
		flexTable2.setBorderWidth(0);
		flexTable2.setWidth("100%");
		
		HorizontalPanel hPanel =new HorizontalPanel();
		Label id = new Label(" ID ");
		id.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		id.setSize("50", "20");
		hPanel.add(id);
		Label tip = new Label(" " + constants.pick()+ " ");
		tip.setSize("35", "20");
		hPanel.add(tip);
		Label odd = new Label(" " + constants.odd()+ " ");
		odd.setSize("35", "20");
		hPanel.add(odd);
		Label erase = new Label(" " + constants.delete()+ " ");
		erase.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		erase.setSize("50", "20");
		hPanel.add(erase);
		flexTable2.setWidget(0, 0, hPanel);
		hPanel.setSize("230", "20");
		
	    ticket = new ScrollPanel();
	    flexTable2.setWidget(1, 0, ticket);
	    ticket.setSize("230", "270");
		flexTable3= new FlexTable();
		ticket.add(flexTable3);
		flexTable3.setWidth("100%");
		
		addPair();

	
	    HorizontalPanel moneyPanel =new HorizontalPanel();
		Label money = new Label(" " + constants.stakePerBet()+ " : ");
		money.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		moneyPanel.add(money);
		money.setSize("170", "20");
		TextBox textBoxBet = new TextBox();
		textBoxBet.setText(stake.toString());
		textBoxBet.addValueChangeHandler(new ValueChangeHandler<String>() {
		    
		    public void onValueChange(ValueChangeEvent<String> event) {
		    	double stake1 =Double.parseDouble(event.getValue());
		    	if (stake1 > 100000.0){
		    		final DialogBox maxStakedialogBox = new DialogBox();
		    		maxStakedialogBox.setText(messages.maxStake());
		    		maxStakedialogBox.setAnimationEnabled(true);
		    		final Button closeButton = new Button("Close");
		    		VerticalPanel dialogVPanel = new VerticalPanel();
		    		dialogVPanel.addStyleName("dialogVPanel");
		    		dialogVPanel.add(new HTML(messages.maxStakeMessage()));
		    		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		    		dialogVPanel.add(closeButton);
		    		maxStakedialogBox.setWidget(dialogVPanel);
		    		maxStakedialogBox.center();
		    		closeButton.setFocus(true);
		    		closeButton.addClickHandler(new ClickHandler() {
		    			public void onClick(ClickEvent event) {
		    				maxStakedialogBox.hide();
		    			}
		    		});
		    	}
		    		
		    	
		    	else {
		    		stake=stake1;
		    		addWinning();
		    	}

		    }
		});

		textBoxBet.setAlignment(TextAlignment.RIGHT);
		textBoxBet.setSize("60", "20");
		
		moneyPanel.add(textBoxBet);
		flexTable2.setWidget(3, 0, moneyPanel);
		moneyPanel.setSize("230", "20");
		
		addOdd();
		addWinning();
		
		
		Button saveSlipButton = new Button(constants.placeBet());
		saveSlipButton.setStyleName("gwt-SignIn");
		flexTable2.setWidget(6, 0, saveSlipButton);
		saveSlipButton.setSize("230", "20");
		
		contentTop = new FlowPanel();
		contentTop.setStyleName("gwt-Template-ContentTop");
		content.add(contentTop);
		

		
		footer = new FlowPanel();
		footer.setStyleName("gwt-Template-footer");
		container.add(footer);
		container.setWidgetLeftWidth(footer, 0.0, Unit.PX, 960.0, Unit.PX);
		container.setWidgetTopHeight(footer, 809.0, Unit.PX, 80.0, Unit.PX);
		labelFooter = new Label(constants.course() + " | Copyright \u00A9 2010 | VirtualBetting");
		footer.add(labelFooter);
			
			
		}
		
	public void isLogged(){
		loginService.isUserLogged(new AsyncCallback<UserDTO>() {
			
			public void onSuccess(UserDTO result) {
            UserDTO user = (UserDTO) result;
            if (user != null) {
            	
            	flexTable.setSize("330px", "35px");
    			
        		username = new Label(user.getUsername());
        		username.setStyleName("gwt-User");
        		//user.setSize("100", "22");
        		flexTable.setWidget(0, 1, username);
        		bankValue = new Label("1234.0 HRK");
        		bankValue.setStyleName("gwt-Money");
        		//bankValue.setSize("100", "22");
        		flexTable.setWidget(0, 2, bankValue);
        		
        		btnProfile = new Button(constants.profile());
        		btnProfile.addClickHandler(new ClickHandler() {
        			public void onClick(ClickEvent event) {
        				interService.checkCurrentLanguage(new AsyncCallback<String>() {

        					@Override
        					public void onFailure(Throwable caught) {
        						Window.alert("Error " + caught.getMessage());
        						
        					}

        					@Override
        					public void onSuccess(String result) {
        						if(result.equals("eng") || result.equals("null")) {
        							Window.open("http://127.0.0.1:8888/Profile.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
        						}
        						else if(result.equals("hr")) {
        							Window.open("http://127.0.0.1:8888/Profile.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
        						}
        						
        					}});
        			}
        		});
        		btnProfile.setStyleName("gwt-SignIn");
        		flexTable.setWidget(0, 3, btnProfile);
        		btnProfile.setSize("60", "22");
        		
        		btnLogOut = new Button(constants.logOut());
        		btnLogOut.addClickHandler(new ClickHandler() {
        			public void onClick(ClickEvent event) {
        				
        				loginService.logout(new AsyncCallback<Void>() {
        					
        					public void onSuccess(Void result) {
        						interService.checkCurrentLanguage(new AsyncCallback<String>() {

        							@Override
        							public void onFailure(Throwable caught) {
        								Window.alert("Error " + caught.getMessage());
        								
        							}

        							@Override
        							public void onSuccess(String result) {
        								if(result.equals("eng") || result.equals("null")) {
        									Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
        								}
        								else if(result.equals("hr")) {
        									Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
        								}
        								
        							}});
        		            }
        		        

        		        public void onFailure(Throwable caught) {
        		        	Window.alert("Error " + caught.getMessage());
        		            

        		        }

        			});
        				
        				
        			}
        		});
        		
        		btnLogOut.setStyleName("gwt-SignIn");
        		btnLogOut.setSize("60", "22");				
        		flexTable.setWidget(0, 4, btnLogOut);
            	
            } else {

    			
    			username = new Label(constants.username());
    			flexTable.setWidget(0, 1, username);
    			
    			textBoxUsername = new TextBox();
    			textBoxUsername.setVisibleLength(10);
    			flexTable.setWidget(0, 2, textBoxUsername);
    			textBoxUsername.setSize("100", "18");
    			
    			password = new Label(constants.password());
    			flexTable.setWidget(0, 3, password);
    			
    			textBoxPassword = new PasswordTextBox();
    			textBoxPassword.setVisibleLength(10);
    			flexTable.setWidget(0, 4, textBoxPassword);
    			textBoxPassword.setSize("100", "18");
    			
    			btnLogIn = new Button(constants.logIn());
    			btnLogIn.addClickHandler(new ClickHandler() {
    				public void onClick(ClickEvent event) {
    					login(textBoxUsername.getText(), textBoxPassword.getText());
    					
    				}

    				private void login (String username, String password) {
    					
    					loginService.checkLogin(username, password,
    						new AsyncCallback<UserDTO>() {
    					
    					public void onSuccess(UserDTO result) {
    		            UserDTO user = (UserDTO) result;
    		            if (user != null) {
    		            	interService.checkCurrentLanguage(new AsyncCallback<String>() {

    							@Override
    							public void onFailure(Throwable caught) {
    								Window.alert("Error " + caught.getMessage());
    								
    							}

    							@Override
    							public void onSuccess(String result) {
    								if(result.equals("eng") || result.equals("null")) {
    									Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
    								}
    								else if(result.equals("hr")) {
    									Window.open("http://127.0.0.1:8888/Bets.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
    								}
    								
    							}});
    		            } else {
    		                Window.alert("Invalid Username or Password");
    		                
    		            }
    		        }

    		        public void onFailure(Throwable caught) {
    		        	Window.alert("Error " + caught.getMessage());
    		            

    		        }

    			});}
    			});
    			
    			btnLogIn.setStyleName("gwt-SignIn");
    			btnLogIn.setSize("60", "22");
    			flexTable.setWidget(0, 6, btnLogIn);
    			
    			
    			
    			btnRegister = new Button(constants.register());
    			btnRegister.addClickHandler(new ClickHandler() {
    				public void onClick(ClickEvent event) {
    					interService.checkCurrentLanguage(new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Error " + caught.getMessage());
								
							}

							@Override
							public void onSuccess(String result) {
								if(result.equals("eng") || result.equals("null")) {
									Window.open("http://127.0.0.1:8888/Register.html?gwt.codesvr=127.0.0.1:9997", "_self", null);
								}
								else if(result.equals("hr")) {
									Window.open("http://127.0.0.1:8888/Register.html?gwt.codesvr=127.0.0.1:9997?locale=hr", "_self", null);
								}
								
							}});
    				}
    			});
    			btnRegister.setStyleName("gwt-SignIn");
    			flexTable.setWidget(0, 7, btnRegister);
    			btnRegister.setSize("70", "22");
        }}

        public void onFailure(Throwable caught) {
        	Window.alert("Error " + caught.getMessage());
        }

	});
	}
}
